package cn.tradewin.advisor.persist.services.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;

import cn.tradewin.advisor.persist.mapper.CodingChkMapper;
import cn.tradewin.advisor.persist.model.CodeLabel;
import cn.tradewin.advisor.persist.model.CodingChk;
import cn.tradewin.advisor.persist.services.CodingChkService;

public class CodingChkServiceImpl implements CodingChkService {

	private final SqlSessionFactory sqlSessionFactory;
	
	public CodingChkServiceImpl(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
	}
	
	@Override
	public List<CodeLabel> getLangOptions() {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CodingChkMapper mapper = session.getMapper(CodingChkMapper.class);
			return mapper.getLangOptions();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<CodeLabel> getCategoryOptions() {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CodingChkMapper mapper = session.getMapper(CodingChkMapper.class);
			return mapper.getCategoryOptions();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<CodingChk> getAllCodingChks() {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CodingChkMapper mapper = session.getMapper(CodingChkMapper.class);
			return mapper.getAllCodingChks();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int addCheckItem(CodingChk item) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CodingChkMapper mapper = session.getMapper(CodingChkMapper.class);
			int count = mapper.insert(item);
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int delete(Integer chkCode) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CodingChkMapper mapper = session.getMapper(CodingChkMapper.class);
			int count = mapper.delete(chkCode);
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
	
	

	@Override
	public List<CodingChk> getCodingChksByLang(Integer lang, Integer category) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CodingChkMapper mapper = session.getMapper(CodingChkMapper.class);
			Map<String, Integer> param = CollectionFactory.newMap();
			param.put("lang", lang);
			param.put("category", category);
			return mapper.getCodingChksByLang(param);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public CodingChk findCodingChk(Integer code) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CodingChkMapper mapper = session.getMapper(CodingChkMapper.class);
			return mapper.findCodingChk(code);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int update(CodingChk record) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			CodingChkMapper mapper = session.getMapper(CodingChkMapper.class);
			int count = mapper.update(record);
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
}
